package org.summer.lawyer.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.summer.common.core.domain.R;
import org.summer.common.core.validate.AddGroup;
import org.summer.common.core.validate.EditGroup;
import org.summer.common.excel.utils.ExcelUtil;
import org.summer.common.idempotent.annotation.RepeatSubmit;
import org.summer.common.log.annotation.Log;
import org.summer.common.log.enums.BusinessType;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.common.web.core.BaseController;
import org.summer.lawyer.domain.vo.spu.SpuCreateVo;
import org.summer.lawyer.domain.vo.spu.SpuUpdateVo;
import org.summer.lawyer.domain.vo.spu.SpuVo;
import org.summer.lawyer.service.ICategoryService;
import org.summer.lawyer.service.ISpuService;

import java.util.List;

/**
 * 商品信息
 *
 * @author summer
 * @date 2024-06-16
 */
@Tag(name = "后台相关接口", description = "后台商品服务信息相关接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lawyer/spu")
public class SpuController extends BaseController {

    private final ISpuService spuService;

    private final ICategoryService categoryService;

    /**
     * 查询商品信息列表
     */
    @Operation(summary = "查询商品信息列表", description = "查询商品信息列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("spu:spu:list")
    @GetMapping("/list")
    public TableDataInfo<SpuVo> list(SpuVo vo, PageQuery pageQuery) {
        return spuService.selectPageList(vo, pageQuery);
    }

    /**
     * 导出商品信息列表
     */
    @Operation(summary = "导出商品信息列表", description = "导出商品信息列表")
    @SaCheckPermission("lawyer:spu:export")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SpuVo vo, HttpServletResponse response) {
        List<SpuVo> list = spuService.selectSpuVoList(vo);
        ExcelUtil.exportExcel(list, "商品信息", SpuVo.class, response);
    }

    /**
     * 获取商品信息详细信息
     *
     * @param spuId 主键
     */
    @Operation(summary = "查询商品信息列表", description = "查询商品信息列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("spu:spu:query")
    @GetMapping("/{spuId}")
    public R<SpuVo> getInfo(@NotNull(message = "主键不能为空")
                            @PathVariable Long spuId) {
        return R.ok(spuService.selectSpuVoById(spuId));
    }

    /**
     * 新增商品信息
     */
    @Operation(summary = "新增商品信息", description = "新增商品信息", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("spu:spu:add")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/saveSpu")
    public R<Void> saveSpu(@Validated(AddGroup.class) @RequestBody SpuCreateVo vo) {
        return toAjax(spuService.insertByVo(vo));
    }

    /**
     * 修改商品信息
     */
    @Operation(summary = "修改商品信息", description = "修改商品信息", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("spu:spu:edit")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/updateSpu")
    public R<Void> updateSpu(@Validated(EditGroup.class) @RequestBody SpuUpdateVo vo) {
        return toAjax(spuService.updateByVo(vo));
    }

    /**
     * 删除商品信息
     *
     * @param spuIds 主键串
     */
    @Operation(summary = "删除商品信息", description = "删除商品信息", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("spu:spu:remove")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{spuIds}")
    public R<Void> removeSpu(@NotEmpty(message = "主键不能为空")
                             @PathVariable Long[] spuIds) {
        return toAjax(spuService.deleteWithValidByIds(List.of(spuIds), true));
    }


//    @Operation(description = "获取服务分类")
//    @SaCheckPermission("lawyer:spu:list")
//    @GetMapping(value = "/categoryTree/{categoryId}")
//    public R<CategoryTreeSelectVo> categoryTree(@PathVariable("categoryId") Long categoryId) {
//        CategoryTreeSelectVo selectVo = new CategoryTreeSelectVo();
//        selectVo.setCheckedKeys();
//        selectVo.setCategories(categoryService.selectCategoryTreeList(new CategoryVo()));
//        return R.ok(selectVo);
//    }
}
